#!/usr/bin/env python
import sys
import os.path

# Redirect the error log of py2exe to the user directory.
# This implementation is based on py2exe/boot_common.py, see
# http://www.py2exe.org/index.cgi/StderrLog for more information.
class Stderr(object):
    softspace = 0
    _file = None
    _error = None
    def write(self, text):
        if self._file is None and self._error is None:
            appdatadir = os.environ.get("APPDATA", "~\\Application Data")
            fname = os.path.join(os.path.expanduser(appdatadir), "MusicBrainz", \
                                 "Picard", os.path.basename(sys.executable) + '.log')
            try:
                self._file = open(fname, 'a')
            except Exception, details:
                self._error = details
                import atexit
                atexit.register(self._show_messagebox_on_exit, "Errors occurred"
                                "The logfile '%%s' could not be opened:\n %%s" %% \
                                    (fname, details))
            else:
                import atexit
                atexit.register(self._show_messagebox_on_exit, "Errors occurred",
                                "See the logfile '%%s' for details" %% fname)
        if self._file is not None:
            self._file.write(text)
            self._file.flush()
    def flush(self):
        if self._file is not None:
            self._file.flush()
    def _show_messagebox_on_exit(self, title, message):
        from PyQt4.QtGui import QApplication, QMessageBox
        QApplication(sys.argv)
        QMessageBox.warning(None, title, message)
sys.stderr = Stderr()
del Stderr

from picard.tagger import main
basedir = os.path.dirname(sys.argv[0])
main(os.path.join(basedir, 'locale'), True)

